/**
 * 波次系统测试工具
 * 自动验证敌人波次系统的正确性
 */
export default class WaveTester {
  constructor(waveManager, characterManager) {
    this.waveManager = waveManager;
    this.characterManager = characterManager;
    this.testRunning = false;
    this.currentTest = null;
    this.testResults = [];
    this.testCases = [
      {
        name: '配置系统测试',
        waves: 1,
        config: { baseEnemies: 5, difficultyCurve: 0.3 },
        check: (wave) => {
          return wave.config.baseEnemies === 5 && 
                 wave.config.difficultyCurve === 0.3;
        }
      },
      {
        name: '敌人生成验证',
        waves: 1,
        check: (wave) => {
          const enemies = this.characterManager.getEnemies();
          return enemies.length > 0 && 
                 enemies.every(e => e.visible) &&
                 enemies.every(e => e.x >= 0 && e.x <= SCREEN_WIDTH - ENEMY_WIDTH) &&
                 enemies.every(e => e.y >= 0 && e.y <= SCREEN_HEIGHT / 3);
        }
      },
      {
        name: '事件系统测试',
        waves: 1,
        check: (wave) => {
          let eventReceived = false;
          const handler = () => eventReceived = true;
          wave.on('waveStart', handler);
          wave.startNextWave();
          wave.eventListeners = wave.eventListeners.filter(l => l.callback !== handler);
          return eventReceived;
        }
      },
      {
        name: '性能监控测试',
        waves: 2,
        check: (wave) => {
          return wave.performance.fps > 0 && 
                 wave.performance.enemyCount >= 0;
        }
      },
      {
        name: '内存管理测试',
        waves: 3,
        check: (wave) => {
          const enemies = this.characterManager.getEnemies();
          const dying = enemies.filter(e => e.isDying).length;
          return dying <= enemies.length;
        }
      }
    ];
  }

  /**
   * 运行所有测试用例
   */
  async runAllTests() {
    console.log('[WaveTest] 开始波次系统测试');
    this.testRunning = true;
    
    for (const testCase of this.testCases) {
      await this.runTest(testCase);
    }
    
    this.testRunning = false;
    this.reportResults();
  }

  /**
   * 运行单个测试用例
   */
  async runTest(testCase) {
    console.log(`[WaveTest] 运行测试: ${testCase.name}`);
    this.currentTest = {
      name: testCase.name,
      waves: testCase.waves,
      startWave: this.waveManager.wave,
      passed: false,
      enemies: 0
    };
    
    // 重置波次状态
    this.waveManager.wave = 0;
    this.waveManager.startFirstWave();
    
    // 等待测试完成
    await new Promise(resolve => {
      const checkInterval = setInterval(() => {
        if (this.waveManager.wave >= testCase.waves) {
          clearInterval(checkInterval);
          
          // 记录最终敌人数量
          this.currentTest.enemies = this.characterManager.getEnemies().length;
          
          // 验证测试条件
          this.currentTest.passed = testCase.check(this.waveManager);
          resolve();
        }
      }, 100); // 每100ms检查一次
    });
    
    this.testResults.push(this.currentTest);
    console.log(`[WaveTest] 测试完成: ${testCase.name} - ${this.currentTest.passed ? '通过' : '失败'}`);
  }

  /**
   * 生成测试报告
   */
  reportResults() {
    console.log('\n=== 波次系统测试报告 ===');
    console.log(`运行测试: ${this.testResults.length}个`);
    
    const passedTests = this.testResults.filter(t => t.passed).length;
    console.log(`通过测试: ${passedTests}/${this.testResults.length}`);
    
    this.testResults.forEach((test, index) => {
      console.log(`\n测试 #${index + 1}: ${test.name}`);
      console.log(`- 测试波次: ${test.waves}`);
      console.log(`- 最终敌人: ${test.enemies}`);
      console.log(`- 状态: ${test.passed ? '通过' : '失败'}`);
    });
    
    if (passedTests === this.testResults.length) {
      console.log('\n所有测试通过! 波次系统工作正常');
    } else {
      console.warn('\n警告: 部分测试未通过，请检查波次系统');
    }
  }

  /**
   * 渲染测试信息
   */
  renderTestInfo(ctx) {
    if (!this.testRunning) return;
    
    ctx.fillStyle = 'rgba(0,0,0,0.7)';
    ctx.fillRect(SCREEN_WIDTH - 210, 10, 200, 80);
    
    ctx.fillStyle = '#FFFF00';
    ctx.font = '14px Arial';
    ctx.textAlign = 'left';
    
    ctx.fillText('测试运行中...', SCREEN_WIDTH - 200, 30);
    ctx.fillText(`当前测试: ${this.currentTest?.name || '-'}`, SCREEN_WIDTH - 200, 50);
    ctx.fillText(`进度: ${this.waveManager.wave}/${this.currentTest?.waves || 0}`, SCREEN_WIDTH - 200, 70);
  }
}